package com.nakanomay.redislock.mylock;

import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;

/**
 * @author nakano_may丶
 * @version 1.0
 * @create 2023-05-09 17:03
 * @description：
 */
@Component
public class DistributedLockFactory
{
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private String lockName;
    private String uuid;

    public DistributedLockFactory()
    {
        this.uuid = IdUtil.simpleUUID();
    }

    public Lock getDistributedLock(String lockType)
    {
        if (lockType == null) {
            return null;
        }

        if (lockType.equalsIgnoreCase("REDIS"))
        {
            this.lockName = "zzyyRedisLock";
            return new RedisDistributedLock(stringRedisTemplate,lockName,uuid);
        } else if (lockType.equalsIgnoreCase("ZOOKEEPER")) {
            this.lockName = "zzyyZookeeperLockNode";
            // todo:zookeeper版本的分布式锁
            return null;
        }else if (lockType.equalsIgnoreCase("MYSQL")) {
            this.lockName = "zzyyMysqlLock";
            // todo:mysql版本的分布式锁
            return null;
        }
        return null;
    }
}
